package com.example.boot.order.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.boot.ResultEnum.ResultEnum;
import com.example.boot.exception.BootException;
import com.example.boot.order.model.Order;
import com.example.boot.order.service.OrderService;
import com.example.boot.util.ResultData;
import com.example.boot.util.ResultUtil;

@RestController
public class OrderController {
	private final static Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private OrderService orderService;

	// @Autowired
	// private RedisUtil redisUtil;

	@RequestMapping(value = "/select/order", method = RequestMethod.GET)
	public Order selectByPrimaryKey(String id) {
		logger.info("selectByPrimaryKey|", id);

		return orderService.selectByPrimaryKey(id);
	}

	@RequestMapping(value = "/commit", method = RequestMethod.GET)
	public ResultData<String> selectByPrimaryKey(@Valid @ModelAttribute Order order, BindingResult bingdingResult)
			throws Exception {
		// logger.info("selectByPrimaryKey|");
		// 抛出的异常都是自动到被HandLe拦截
		if (order.getNumber() == null) { // 如果是别的异常
			throw new RuntimeException("我才");
			// throw new BootException(0, "我菜哦");
		}
		if (bingdingResult.hasErrors()) {// 自己的异常。
			throw new BootException(ResultEnum.A_MINOR);
		}

		orderService.insertSelective();

		return ResultUtil.success("成功");
	}

	@RequestMapping(value = "/test/redis", method = RequestMethod.GET)
	public ResultData<Object> getKey(String key, String value) {
		Object object2 = orderService.get(key, value);
		// RedisUtil redisUtil = new RedisUtil();

		return ResultUtil.success(object2);
	}

	@RequestMapping(value = "/test2", method = RequestMethod.GET)
	public String tests(String testStr, @ModelAttribute Order order) {

		return "22";

	}
}
